package cn.bby.jtk.base.task;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.function.Supplier;

/**
 * @author ben
 * @since 2025/4/17 00:15
 */
public class AsyncUtil {

    private static ExecutorService executorService;

    public static void initExecutorService(ExecutorService executorService) {
        AsyncUtil.executorService = executorService;
    }

    public static CompletableFuture<?> runAsync(Runnable runnable) {
        return executorService != null ? CompletableFuture.runAsync(runnable, executorService)
                : CompletableFuture.runAsync(runnable);
    }

    public static <T> CompletableFuture<T> runAsync(Supplier<T> task) {
        return executorService != null ? CompletableFuture.supplyAsync(task, executorService)
                : CompletableFuture.supplyAsync(task);
    }

    public static void getExecutorService() {
        if (executorService == null) {
            throw new RuntimeException("ExecutorService is not initialized");
        }
    }

}
